package com.example.goandroiddemo.animate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.Toast;

import com.chouren.goandroid.animate.Animations;
import com.chouren.goandroid.util.Logger;
import com.example.goandroiddemo.R;

public class AnimationTestDemo extends Activity {
	private Button mButtonTest;

	int width;
	int height;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Logger.debug(null, "onCreate");
		setContentView(R.layout.activity_animate_demo);
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void onResume() {
		Logger.debug(null, "onResume");
		super.onResume();
	}
	
	@Override
	protected void onStart() {
		Logger.debug(null, "onStart");
		runanimate();
		super.onStart();
	}

	@Override
	protected void onRestart() {
		Logger.debug(null, "onRestart");
		super.onRestart();
	}
	
	@Override
	public void onAttachedToWindow() {
		Logger.debug(null, "onAttachedToWindow");
		super.onAttachedToWindow();
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		Logger.debug(null, "onWindowFocusChanged");
//		runanimate();
		super.onWindowFocusChanged(hasFocus);
	}
	
	@Override
	public void onWindowAttributesChanged(LayoutParams params) {
		Logger.debug(null, "onWindowAttributesChanged");
		super.onWindowAttributesChanged(params);
	}

	
	public void runanimate() {
		mButtonTest = (Button) findViewById(R.id.test);

		mButtonTest.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(AnimationTestDemo.this, "haha",
						Toast.LENGTH_SHORT).show();
			}
		});

		final float fromX = mButtonTest.getLeft();
		final float fromY = mButtonTest.getTop();
		 final float toX = fromX + 100;
		 final float toY = fromY - 100;

//		final float toX = 0;
//		final float toY = 267;

		final int marginLeft = (int) toX;
		final int marginTop = (int) toY;
		final int marginRight = 0;
		final int marginBottom = 0;

		int durationMillis = 3000;

		Logger.debug(null, "fromX:" + fromX + ";fromY:" + fromY + "toX:" + toX
				+ "toY:" + toY);
		Logger.debug(null, "marginLeft:" + marginLeft + ";marginTop:"
				+ marginTop + "marginRight:" + marginRight + "marginBottom:"
				+ marginBottom);

		Animation anim = Animations.animTranslate(fromX, toX, fromY, toY,
				durationMillis);
		anim.setFillAfter(true);
		mButtonTest.startAnimation(anim);
		anim.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// // runBack();
				// LinearLayout.LayoutParams params = new
				// LinearLayout.LayoutParams(0, 0);
				// params.height = mButtonTest.getHeight();
				// params.width = mButtonTest.getWidth();
				//
				//
				// int marginLeft1 = (int)(mButtonTest.getLeft() + (toX -
				// fromX));
				// int marginTop1 = (int)(mButtonTest.getTop() + (toY - fromY));
				//
				// Logger.warn(null, "height:" + height + ";width:" + width );
				// Logger.warn(null, "marginLeft:" + marginLeft + ";marginTop:"
				// + marginTop +
				// "marginRight:" + marginRight + "marginBottom:" +
				// marginBottom);
				// Logger.warn(null, "marginLeft:" + marginLeft1 + ";marginTop:"
				// + marginTop1 +
				// "marginRight:" + marginRight + "marginBottom:" +
				// marginBottom);
				//
				// params.setMargins(marginLeft1, marginTop1, marginRight,
				// marginBottom);
				// mButtonTest.setLayoutParams(params);
				// mButtonTest.clearAnimation();
			}
		});
	}

	public void runBack() {
		float fromX1 = 100;
		float fromY1 = 100;
		float toX1 = mButtonTest.getLeft();
		float toY1 = mButtonTest.getTop();

		int marginLeft1 = 0;
		int marginTop1 = 0;
		int marginRight1 = 0;
		int marginBottom1 = 0;

		int durationMillis1 = 3000;

		Logger.debug(null, "fromX:" + fromX1 + ";fromY:" + fromY1 + "toX:"
				+ toX1 + "toY:" + toY1);
		Logger.debug(null, "marginLeft:" + marginLeft1 + ";marginTop:"
				+ marginTop1 + "marginRight:" + marginRight1 + "marginBottom:"
				+ marginBottom1);

		Animation anim1 = Animations.animTranslate(fromX1, toX1, fromY1, toY1,
				durationMillis1);
		anim1.setFillAfter(true);
		mButtonTest.startAnimation(anim1);
		anim1.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
			}
		});
	}
}
